
/*

Design Pattern Automation Toolkit.
Application to create applications with emphasis on Design patterns.
And support for round trip engineering.
Copyright (C) 2004 Vineeth Neelakant. nvineeth@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
using System;
using BusinessLayer;
using InfoClasses;
using dpatoolkit.UILayer;
using System.Data;
using System.Collections.Generic;

namespace BusinessLayer
{
	/// <summary>
	/// Summary description for PluginDataStructure.
	/// </summary>
	public class PluginDataStructure : ApplicationDataSet , IDesignPattern
	{
		protected DataTable miscTable;

		public PluginDataStructure() : base()
		{
			this.DPAppDataSet.DataSetName = SchemaNames.DesignPatternPluginTag;
			this.miscTable = this.DPAppDataSet.Tables[SchemaNames.Misc];
		}

		#region IDesignPattern Members
        public System.Collections.Generic.IDictionary<string, string> DesignPatternDetails 
        {
            get
            {
                // Construct the Design Pattern Details for the rows;
                IDictionary<string, string> desPatDetails = new Dictionary<string, string>();

                foreach (DataRow row in this.miscTable.Rows)
                {
                    desPatDetails[(string)row[SchemaNames.Detail]] = (string)row[SchemaNames.Info];
                }
                return desPatDetails;
            }
            set
            {
                // Set the contents of the desingn pattern details fro mthe dictionary;
                this.miscTable.Rows.Clear();
                foreach (KeyValuePair<string, string> kvp in value)
                {
                    DataRow row = this.miscTable.NewRow();
                    row[SchemaNames.Detail] = kvp.Key ;
                    row[SchemaNames.Info]   = kvp.Value;
                    this.miscTable.Rows.Add(row);
                }
            }
        }

        public void ClearDetails()
        {
            this.miscTable.Clear();
        }

        public string this[string dpProperty]
        { 
            get
            {
                DataRow row = this.miscTable.Rows.Find(dpProperty);
                if (row != null)
                    return row[SchemaNames.Info] as string;
                return null;
            }
            set
            {
                DataRow findrow = this.miscTable.Rows.Find(dpProperty), row;
                if (findrow == null)
                    row = miscTable.NewRow();
                else
                    row = findrow;
                row[SchemaNames.Detail] = dpProperty;
                row[SchemaNames.Info] = value;
                try
                {
                    if (findrow == null)
                        this.miscTable.Rows.Add(row);
                }
                catch (Exception)
                {
                    //ErrorMessage.Show("This Detail already present", "Error");
                }
                return;
            }
        }
		

		public bool IsPattern()
		{
			DataRow [] row = this.DPAppDataSet.Tables[SchemaNames.Components].Select();
			if( row.Length != 1 ) return false;
			if ( (string)row[0][SchemaNames.ComponentName] != PluginEditorForm.pluginTag )
				return false;
			return true;
		}
		

		#endregion
	}
}
